<?php
/**
 * WebmasterCity V3
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category	WebmasterCity V3
 * @package	 Authentication
 * @copyright   (c) Copyright 2009-2010 by wmcity.nl
 *
 * $Id: Doctrine.php 214 2009-06-18 21:24:03Z luckyluke56 $
 */

require_once 'WMC/Auth/Adapter/Doctrine/Table.php';

/**
 * This class allows you to authenticate a user using a doctrine table
 * class.
 *
 * The given class should implement WMC_Auth_Adapter_Doctrine_Table,
 * which defines a method checkCredentials($username, $pass);
 *
 * This function should return the correspondending Doctrine_Result instance,
 * so use execute(), not fetchOne().
 */
class WMC_Auth_Adapter_Doctrine implements Zend_Auth_Adapter_Interface
{
	/**
	 * The Doctrine table class
	 * @var Doctrine_Table
	 */
	protected $table;

	/**
	 * Holds the username
	 * @var string
	 * @see WMC_Auth_Adapter_Doctrine::setIdentity()
	 */
	protected $identity;

	/**
	 * Holds the password
	 * @var string
	 * @see WMC_Auth_Adapter_Doctrine::setCredential()
	 */
	protected $credential;

	/**
	 * Holds the row object if authentication is successful
	 * @var Doctrine_Record
	 * @see WMC_Auth_Adapter_Doctrine::getResultRow()
	 */
	protected $resultRow;

	public function __construct($tableClass, $username = null, $password = null)
	{
		// Get table
		$table = Doctrine::getTable($tableClass);

		if(!$table instanceof WMC_Auth_Adapter_Doctrine_Table)
		{
			throw new Zend_Auth_Adapter_Exception('Table '.$tableClass.' is not a subclass of WMC_Auth_Adapter_Doctrine_Table');
		}

		$this -> table = $table;

		// Set given properties
		if($username !== null)
		{
			$this -> setIdentity($username);
		}

		if($password != null)
		{
			$this -> setCredential($password);
		}
	}

	public function setIdentity($user)
	{
		$this -> identity = $user;
		return $this;
	}

	public function setCredential($pass)
	{
		$this -> credential = $pass;
		return $this;
	}

	public function getResultRow()
	{
		return $this -> resultRow;
	}

	public function authenticate()
	{
		$this -> authSetup();

		$result = $this -> table -> checkCredentials($this -> identity, $this -> credential);

		if(count($result) > 1)
		{
			$this -> authResult['code'] = Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS;
			$this -> authResult['messages'][] = 'One or more records has been found using the given credentials';
		}
		else if(count($result) < 1)
		{
			$this -> authResult['code'] = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND;
			$this -> authResult['messages'][] = 'No account matches the given credentials';
		}
		else
		{
			$this -> authResult['code'] = Zend_Auth_Result::SUCCESS;
		}

		$this -> resultRow = $result[0];

		return $this -> createAuthResult();
	}

	protected function authSetup()
	{
		if(empty($this -> identity) || empty($this -> credential))
		{
			throw new Zend_Auth_Adapter_Exception('No username or password given');
		}

		$this -> authResult = array(
			'code' => Zend_Auth_Result::FAILURE,
			'identity' => $this -> identity,
			'messages' => array()
		);
	}

	protected function createAuthResult()
	{
		return new Zend_Auth_Result($this -> authResult['code'], $this -> authResult['identity'], $this -> authResult['messages']);
	}
}
